<?php
include 'bbs_invite_code_remote.php';

function getIP()
{
    static $realip;
    if (isset($_SERVER)) {
        if (isset($_SERVER["HTTP_X_FORWARDED_FOR"])) {
            $realip = $_SERVER["HTTP_X_FORWARDED_FOR"];
        } else if (isset($_SERVER["HTTP_CLIENT_IP"])) {
            $realip = $_SERVER["HTTP_CLIENT_IP"];
        } else {
            $realip = $_SERVER["REMOTE_ADDR"];
        }
    } else {
        if (getenv("HTTP_X_FORWARDED_FOR")) {
            $realip = getenv("HTTP_X_FORWARDED_FOR");
        } else if (getenv("HTTP_CLIENT_IP")) {
            $realip = getenv("HTTP_CLIENT_IP");
        } else {
            $realip = getenv("REMOTE_ADDR");
        }
    }
    return $realip;
}

$ip = getIP();

$mode = $_POST['mode'];
$indexs = explode(',', $_POST['indexs']);
$ans = explode(',', $_POST['ans']);
$len = count($ans);
$score = 100;
$pity = $score / $len;
$db = local_db();
if ($mode == 'easy'){
    $sheet = 'questions';
}
if ($mode == 'hard'){
    $sheet = 'questions_hard';
}

for ($i = 0; $i < $len; $i++) {
    $sql = "SELECT * FROM `$sheet` INNER JOIN `questions_con` ON `$sheet`.`contribute_No`=`questions_con`.`No` WHERE `$sheet`.`QNo`=$indexs[$i]";
    $result = $db->query($sql);
    if ($result->num_rows > 0) {
        while ($row = $result->fetch_assoc()) {
            $score -= ($row['bingo'] == $ans[$i]) ? 0 : $pity;
        }
    }
}
$score = round($score);

class checker
{
    function __construct($score, $ip, $mode)
    {
        $this->score = $score;
        $this->ip = $ip;
        $this->text = '';
        $this->date = date("Y-m-d");
        $this->status = 0;
        if ($this->score < 52) $this->credit = 21;
        else if($this->score < 68) $this->credit = 221;
        else if($this->score < 84) $this->credit = 521;
        else $this->credit = 1021;
        if ($mode == 'easy'){
            $this->check();
        }
        if ($mode == 'hard'){
            $this->only_score();
        }
    }
    function only_score(){
        $this->status = 0;
        if ($this->score >= 90) {
            $this->text = "哇！好厉害~厨力已经爆表了！<br>一定是Key的真真真真真爱粉OvO！";
        }else if ($this->score >= 70) {
            $this->text = "诶嘿~你一定是一个很细心的键子哦~<br><del>稻荷才不会告诉你台长也只答了80分</del>";
        }else if ($this->score >= 50) {
            $this->text = "嘭！要再接再厉呢~<br>罚你去推一千遍Key全家桶！";
        }else{
            $this->text = "你到底有没有在认真答题呀QAQ<br>再这样下去你假键子的身份就要暴露啦！<br>稻荷很担心你呢~";
        }
    }
    function check()
    {
        if ($this->score >= 52) {
            if ($this->ip_check()) {
                $this->code = $this->get_code($this->credit);
                if ($this->code) {
                    $this->text = "您获得了面值为".$this->credit."稻荷币的邀请码！<br>";
                    $this->text .= "邀请码有效期至当日晚23:59:59<br>";
                    $this->status = 1;
                } else {
                    $this->text = "呜呜呜稻荷登记邀请码的时候出了一点小问题qaq<br>快去联系管理员吧~";
                    $this->status = 404;
                }
            } else {
                $this->text = "残念~每天同一ip地址只可以获得一个邀请码哦~";
                $this->status = 2;
            }
        } else {
            $this->text = "呜呜呜TwT 再多努力一下下<br>就可以一起玩耍啦~";
            $this->status = 0;
        }
    }
    function ip_check()
    {
        $db = local_db();
        $sheet = 'invite_code';
        $sql = "SELECT * FROM `$sheet` WHERE ipAddress='$this->ip' AND date='$this->date'";
        $result = $db->query($sql);
        if ($result->num_rows > 0) {
            while ($row = $result->fetch_assoc()) {
                return false;
            }
        } else return true;
    }
    function get_code($credit)
    {
        //return false;
        $inv_code = generate($credit);
        $id = $inv_code['id'];
        $code = $inv_code['code'];
        if ($code != null) {
            $db = local_db();
            $sheet = 'invite_code';
            $sql = "INSERT INTO `$sheet` (`id`, `code`, `date`,`ipAddress`) VALUES ('$id','$code','$this->date','$this->ip')";
            $result = $db->query($sql);
            return $code;
        } else {
            return false;
        }
        /*$r = $db->query($sql);
        return true;
        $sql = "SELECT * FROM `$sheet` WHERE status=0";
        $result = $db->query($sql);
        if ($result->num_rows > 0) {
            $row = $result->fetch_assoc();
            $code = $row['code'];
            $index = $row['No'];
            $sql = "UPDATE `$sheet` SET `status` = 1 , `date` = '$this->date' WHERE `invite_code`.`No` = $index";
            $r= $db->query($sql);
            return $code;
        }
        else {
            return false;
        }*/
    }
}
$return_val = new checker($score, $ip, $mode);
echo json_encode($return_val);
?>